/**
 * @description: 文件描述
 * @version: 0.0.1
 * @author: xxx
 * @Date: 2025-01-21 15:01:00
 */

let { trackMoreFun, globalData } = getApp();
Component({
  data: {
    mainGoods: {},  // 主图
    relatedArr: [],// 推荐商品
    relatedImgH: 618,
  },
  async didMount() {
    try {
      globalData.pageNum = 4
      trackMoreFun('预览_发现你的嗅觉个性_商品预览页面','into' )
      const xclskus_sort = await this.getStorage('xclskusArr_sort')
      let relatedArr = []
      let firstRelatedSkuArr = []
      let mainGoods = {}
      let relatedImgH = 618
      if (xclskus_sort.length && xclskus_sort[0] && xclskus_sort[0].relatedSku) {
        const scoreArr = xclskus_sort.filter(i => i.score == xclskus_sort[0].score)
        if (scoreArr.length === 1) {
          mainGoods = scoreArr[0]
          firstRelatedSkuArr = scoreArr[0].relatedSku
        } else if (scoreArr.length > 1) {
          // score 相同时 取rank值最大的
          scoreArr.sort((a, b) => {
            return b.rank - a.rank
          })
          mainGoods = scoreArr[0]
          firstRelatedSkuArr = scoreArr[0].relatedSku
        }
        relatedArr = firstRelatedSkuArr.flatMap(t => xclskus_sort.filter(i => t == i.sku_id))
        console.log(relatedArr, 'relatedArr')
        if (relatedArr[0].lineNum == 2 || relatedArr[1].lineNum == 2) {
          relatedArr[0].recommendGoodsImgUrl = relatedArr[0].recommendGoodsImgUrl
          relatedArr[1].recommendGoodsImgUrl = relatedArr[1].recommendGoodsImgUrl
        } else {
          relatedImgH = 582
          relatedArr[0].recommendGoodsImgUrl = relatedArr[0].recommendGoodsImgUrl2
          relatedArr[1].recommendGoodsImgUrl = relatedArr[1].recommendGoodsImgUrl2
        }
      }

      this.setData({
        mainGoods,
        relatedArr: relatedArr,
        relatedImgH
      })
      console.log(this.data.relatedArr, 'relatedArrrelatedArr')
    } catch (error) {
      console.log(error, 'sacascxzczxcz')
    }
  },
  methods: {
    // 主图-即刻珍藏
    onCollectNow(e) {
      const { itemId = '', skuId = '', goodsName } = e.currentTarget.dataset.item
      trackMoreFun(`点击_发现你的嗅觉个性_主商品-即刻珍藏_${goodsName}`, 'click')
      if (itemId) my.tb.showSku({ itemId, skuId });
    },
    // 推荐-即刻珍藏
    onCollectNowTreasure(e) {
      const { itemId = '', skuId = '', goodsName } = e.currentTarget.dataset.item
      trackMoreFun(`点击_发现你的嗅觉个性_推荐商品-即刻珍藏_${goodsName}`, 'click')
      if (itemId) my.tb.showSku({ itemId, skuId });
    },
    // 主图-分享体验
    onShare(e) {
      const { goodsName } = e.currentTarget.dataset.item
      trackMoreFun(`点击_发现你的嗅觉个性_主商品-分享体验_${goodsName}`, 'click')
      my.showSharePanel();
    },
    // 主图-商品详情
    toGoodsDetails(e) {
      const { itemId = '', skuId = '', goodsName } = e.currentTarget.dataset.item
      trackMoreFun(`点击_发现你的嗅觉个性_主商品-跳转详情_${goodsName}`, 'click')
      if (itemId) { } my.tb.openDetail({ itemId, skuId });
      globalData.pageState = true
    },
    // 推荐-商品详情
    toGoodsDetailsTreasure(e) {
      const { itemId = '', skuId = '', goodsName } = e.currentTarget.dataset.item
      trackMoreFun(`点击_发现你的嗅觉个性_推荐商品_跳转详情_${goodsName}`, 'click')
      if (itemId) { } my.tb.openDetail({ itemId, skuId });
      globalData.pageState = true
    },
    getStorage(key) {
      return new Promise((resolve, reject) => {
        my.getStorage({
          key,
          success: function (res) {
            resolve(res.data)
          },
          fail: function (res) {
            reject([])
          }
        });
      })
    },
  },

})
Page({




});